package com.hejian.android.a72ggametools.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.INotificationSideChannel;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.hejian.android.a72ggametools.R;
import com.hejian.android.a72ggametools.fragment.ActionFragment;
import com.hejian.android.a72ggametools.fragment.ConvertFragment;
import com.hejian.android.a72ggametools.fragment.GiftFragment;
import com.hejian.android.a72ggametools.fragment.MineFragment;
import com.hejian.android.a72ggametools.fragment.MoneyFragment;

import java.util.ArrayList;


/**
 * Created by Administrator on 2016/1/7.
 */
public class MainActivity extends AppCompatActivity {
    private static final String TAG = "hejiandebug";
    private RadioGroup radioGroup;
    private ArrayList<Fragment> fragmentList;
    private FragmentManager manager;
    private FragmentTransaction transition;
    private FrameLayout mainFramLayout;
    private boolean isNotFirstIn = false;
    private LinearLayout imgSelectLL;
    private Animation animation;
    private Intent intent;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intent = getIntent();
        initView();
        if (intent != null) {
            isNotFirstIn = intent.getBooleanExtra("hasdata", false);
            if (isNotFirstIn) {
                String sign = intent.getStringExtra("sign");
                if (sign.equals("fromlogin") || sign.equals("fromcheck")) {
                    setCurrentFragment(4);
                    setCurrentAnimation((radioGroup.getChildAt(4)).getId());
                }
            }
        }
        setListener();


    }

    private void setListener() {
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                setRadioGroupChecked(checkedId);
                setCurrentAnimation(checkedId);
            }
        });
    }

    private void setCurrentAnimation(int checkedId) {
        for (int i = 0, len = radioGroup.getChildCount(); i < len; i++) {
            RadioButton currentButton = (RadioButton) radioGroup.getChildAt(i);
            ImageView currentImg = (ImageView) imgSelectLL.getChildAt(i);
            if (currentButton.getId() == checkedId) {
                currentButton.setVisibility(View.INVISIBLE);
                currentImg.setVisibility(View.VISIBLE);
                currentImg.startAnimation(animation);
            } else {
                currentButton.setVisibility(View.VISIBLE);
                currentImg.setVisibility(View.INVISIBLE);
            }
        }

    }

    private void setRadioGroupChecked(int checkedId) {
        Log.d(TAG, "setRadioGroupChecked: " + checkedId);
        mainFramLayout.removeAllViews();
        radioGroup.check(checkedId);
        setCurrentFragment(checkedId - 1);

    }

    private void initView() {
        radioGroup = (RadioGroup) findViewById(R.id.radio_select);
        animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.animation_main);
        radioGroup.check(radioGroup.getChildAt(0).getId());
        mainFramLayout = (FrameLayout) findViewById(R.id.fragment_mian_container);
        imgSelectLL = (LinearLayout) findViewById(R.id.image_select);

        setCurrentFragment(0);
        setCurrentAnimation(1);

    }


    private void initFragment() {
        fragmentList = new ArrayList<Fragment>();
        MoneyFragment moneyFragment = new MoneyFragment();
        fragmentList.add(moneyFragment);
        GiftFragment giftFragment = new GiftFragment();
        fragmentList.add(giftFragment);
        ActionFragment actionFragment = new ActionFragment();
        fragmentList.add(actionFragment);
        ConvertFragment convertFragment = new ConvertFragment();
        fragmentList.add(convertFragment);
        MineFragment mineFragment = new MineFragment();
        fragmentList.add(mineFragment);
    }

    private void setCurrentFragment(int position) {
        initFragment();
        if (fragmentList != null) {
            manager = getSupportFragmentManager();
            transition = manager.beginTransaction();
            Fragment currentFragment = fragmentList.get(position);
            if (intent.getBooleanExtra("hasdata", false)) {
                Bundle bundle = intent.getBundleExtra("userbundle");
                if (bundle != null) {
                    currentFragment.setArguments(bundle);
                }
            }
            transition.replace(R.id.fragment_mian_container, currentFragment);
            transition.commit();
        }
    }


}
